package monitor;

import java.text.DecimalFormat;
import java.util.Timer;
import java.util.TimerTask;

import com.jcraft.jsch.SftpProgressMonitor;

public class FileProgressMonitor extends TimerTask implements SftpProgressMonitor {

	private long progressInterval = 5 * 10;//默认间隔时间为5秒
	private boolean isEnd = false;//记录传输是否结束
	private long transfered;//记录已传输的数据总大小
	private long fileSize; //记录文件总大小
	private Timer timer; //定时器对象
	
	private boolean isScheduled = false;//记录是否已启动timer计时器
	
	public FileProgressMonitor(long fileSzie) {
		this.fileSize = fileSzie;
	}
	
	@Override
	public void init(int op, String src, String dest, long max) {

	}

	@Override
	public boolean count(long count) {
		if(isEnd()){
			return false;
		}
		if(!isScheduled){
			start();
		}
		add(count);
		return true;
	}

	@Override
	public void end() {
		setEnd(true);
		System.out.println("transfering end.");
	}

	@Override
	public void run() {

		if(!isEnd()){
			System.out.println("Transfering is in progress.");
			long transfered = getTransfered();
			if(transfered != fileSize){// 判断当前已传输数据大小是否等于文件总大小
				System.out.println("Current transfered: " + transfered + " bytes");
				sendProgressMessage(transfered);
			}else {
				System.out.println("File transfering is done.");
                setEnd(true); // 如果当前已传输数据大小等于文件总大小，说明已完成，设置end
			}
		}else{
			System.out.println("Transfering done. Cancel timer.");
			stop(); // 如果传输结束，停止timer记时器
            return;
		}
	}


	/**
	 * 打印progress信息
	 * @param transfered2
	 * @author yutao
	 * @date 2017年4月24日下午3:28:31
	 */
	private void sendProgressMessage(long transfered) {
		if(fileSize != 0){
			double d = ((double)transfered * 100)/(double)fileSize;
			DecimalFormat df = new DecimalFormat("#.##");
			System.out.println("Sending progress message: " + df.format(d) + "%");
		}else{
			System.out.println("Sending progress message: " + transfered);
		}
	}


	private void stop() {
		System.out.println("Try to stop progress monitor.");
		if(timer != null){
			timer.cancel();
			timer.purge();
			timer = null;
			isScheduled = false;
		}
		System.out.println("Progress monitor stoped.");
	}
	
	
	public void start(){
		System.out.println("Try to start progress monitor.");
		if(timer == null){
			timer = new Timer();
		}
		timer.schedule(this, progressInterval);
		isScheduled = true;
		System.out.println("Progress monitor started.");
	}

	private void add(long count) {
        transfered = transfered + count;
    }
	
	private long getTransfered() {
        return transfered;
    }
    
    public void setTransfered(long transfered) {
        this.transfered = transfered;
    }
    
    private void setEnd(boolean isEnd) {
        this.isEnd = isEnd;
    }
    
    private boolean isEnd() {
        return isEnd;
    }
	
}
